﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Skywave.Reporting
{
    public class HtmlReportFieldRepeater : HtmlReportField
    {

        private bool _AutoPageBreak = true;
        /// <summary>
        /// ...
        /// </summary>
        public bool AutoPageBreak
        {
            get { return _AutoPageBreak; }
            set { _AutoPageBreak = value; }
        }

        private string _Content;
        /// <summary>
        /// ...
        /// </summary>
        public string Content
        {
            get { return _Content; }
            set { _Content = value; }
        }

        internal override string GetContent(HtmlReportFieldGetContentArgs e)
        {
            string r = "";
            string r_Item;
            HtmlReportFieldGetContentArgs e_Clone = e.Clone() as HtmlReportFieldGetContentArgs;
            //
            if (e.Value is IEnumerable)
            {
                //foreach (var fe1 in e.DataItem as IEnumerable)
                //{
                //    r_Item = _Content;
                //    foreach (var fePF in ParsedFields.Where(q => r_Item.Contains(q.ParsedText_ReplaceKey)))
                //    {
                //        e_Clone.Value = e.Report.GetFieldValue(fePF.Field, e.DataItem);
                //        r_Item = r_Item.Replace(fePF.ParsedText_ReplaceKey, fePF.Field.GetContent(e_Clone));
                //    }
                //    //
                //    r += r_Item;
                //}
                List<object> dataList = (e.Value as IEnumerable).Cast<object>().ToList();
                int iDataItem = 1;
                foreach (object feDataItem in dataList)
                {
                    e_Clone.DataItem = feDataItem;
                    r_Item = _Content;
                    //
                    int iField = 1;
                    foreach (HtmlReportParsedField feParsedField in ParsedFields.Where(q => r_Item.Contains(q.ParsedText_ReplaceKey)))
                    {
                        e_Clone.Value = e.Report.GetFieldValue(feParsedField.Field, e_Clone.DataItem);
                        //
                        e_Clone.IndexDataItem = iDataItem;
                        e_Clone.IndexField = iField;
                        r_Item = r_Item.Replace(feParsedField.ParsedText_ReplaceKey, feParsedField.Field.GetContent(e_Clone));
                        //
                        if (e_Clone.IndexField > iField)
                            iField = e_Clone.IndexField;
                        //
                        iField++;
                    }
                    //
                    r += string.Format(@"
                <div style='width: 100%; {1} clear: both;'>
                    {0}
                </div>", r_Item, (AutoPageBreak == false || iDataItem == dataList.Count) ? "" : "page-break-after: always;");
                    //
                    iDataItem++;
                }
            }
            //
            return r;
        }

        internal override void Create_Internal(string toParse)
        {
            using (System.IO.StringReader sr1 = new System.IO.StringReader(HtmlReport.Parsing_FixToParse(toParse)))
            {
                System.Xml.XmlReader xmlR1 = HtmlReport.Parsing_GetXmlReader(sr1);
                //
                while (xmlR1.Read())
                {
                    if (xmlR1.Depth == 0 && xmlR1.IsStartElement("ReportField", "http://skywavetech.ir/skywave/reporting"))
                    {
                        _Content = xmlR1.ReadInnerXml();
                        break;
                    }
                }
            }
        }
    }
}
